<template>
  <div class="app-container">
    <!-- 查询 -->
    <tl-list-search style="margin:6px 0" ref="search" :query-param="queryParam" @search="doQuery" :isDisplayDefaultSearch="false" v-if="isHeaderDisplay">
      <template slot="search">
        <el-form-item class="qryItem" label="项目名称" style="margin-left:-12px">
          <el-input v-model="queryParam.qry_projectname" placeholder="关键字" @keyup.enter.native="doQuery"></el-input>
        </el-form-item>
        <el-form-item class="qryItem" label="任务名称">
          <el-input v-model="queryParam.qry_taskname" placeholder="关键字" @keyup.enter.native="doQuery"></el-input>
        </el-form-item>
        <el-form-item class="qryItem" label="里程碑名称">
          <el-input v-model="queryParam.qry_milepostname" placeholder="关键字" @keyup.enter.native="doQuery"></el-input>
        </el-form-item>
        <el-form-item class="qryItem" label="任务状态">
          <el-select v-model="taskstateList" multiple placeholder="请选择项目状态" style="width:240px">
            <el-option v-for="item in codeModel.taskstateList" :key="item.code" :label="item.name" :value="item.code"> </el-option>
          </el-select>
        </el-form-item>
      </template>
    </tl-list-search>
    <!-- 工具栏 -->
    <!-- <tl-list-toolbar ref="toolbar" @add="openAddDialog('editForm')" @exportExcel="doExportExcel" :isDisplayExport="false" :isDisplayAdd="false" :isDisplayDeleteBatch="false" @deleteBatch="doDeleteBatch" @refresh="doRefresh" @columnCustomize="doColumnCustomize" @displaySearch="doDisplaySearch" :disableDeleteBatch="selectedRowKeys.length <= 0">
    </tl-list-toolbar> -->
    <div>
      <!-- 列表选择 -->
      <div class="table-alert-div" ref="selector">
        <el-alert type="success" :closable="false" class="tl-table-alert-info" effect="light">
          <template slot="title">
            已选择&nbsp;<a class="tl-table-alert-info-a1">{{ selectedRowKeys.length }}</a
            >项&nbsp;&nbsp;
            <a class="tl-table-alert-info-a2" @click="doClearSelected">全部清空</a>
          </template>
        </el-alert>
      </div>
      <!--表格渲染-->
      <el-table class="tl-table" ref="mainTable" :height="tableHeight" resizable-key="tableHeight" :resizable-refs="tableResizeRefs" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" @row-click="doRowClick" row-key="id" border fit :row-class-name="doChangeRowClassName">
        <el-table-column type="index" label="序号" width="50px" align="center"></el-table-column>
        <!-- <el-table-column prop="ordercode" label="序号" width="150px" sortable="custom" sort-by="ordercode" align="center"
          header-align="center" :show-overflow-tooltip="true"></el-table-column> -->
        <el-table-column prop="projectcode" label="项目编号" width="130px" sortable="custom" sort-by="projectcode" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectname" label="项目名称" min-width="150px" sortable="custom" sort-by="projectname" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="dispprojectlevel" label="项目级别" width="80px" sortable="custom" sort-by="projectlevel" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectmanager" label="项目经理" width="80px" sortable="custom" sort-by="projectmanager" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="milepostname" label="里程碑名称" width="200px" sortable="custom" sort-by="milepostname" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="taskname" label="任务名称" width="200px" sortable="custom" sort-by="taskname" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="disptaskstate" label="任务状态" width="80px" sortable="custom" sort-by="taskstate" align="center" header-align="center" :show-overflow-tooltip="true">
          <template v-slot="scope">
            <el-tag :type="changeStateType(scope.row.taskstate)">
              {{ scope.row.disptaskstate }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="taskschedule" label="任务进度" width="135px" sortable="custom" sort-by="taskschedule" align="center" header-align="center" :show-overflow-tooltip="true" :formatter="percentageFormat">
          <template v-slot="scope">
            <template v-if="istaskapproval(scope.row) && !checkTaskScheduleIsNull(scope.row)">{{scope.row.taskschedule + '%' + '(100%待审批)'}}</template>
            <template v-else-if="istaskapproval(scope.row) && checkTaskScheduleIsNull(scope.row)">{{'0%(100%待审批)'}}</template>
            <template v-else-if="checkTaskScheduleIsNull(scope.row)">{{'0%'}}</template>
            <template v-else>{{scope.row.taskschedule + '%'}}</template>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="disptasktype" label="任务类型" width="100px" sortable="custom" sort-by="tasktype"
          align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="disptaskpriority" label="任务优先级" width="100px" sortable="custom" sort-by="taskpriority"
          align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column> -->
        <!-- <el-table-column prop="taskdetail" label="任务内容" width="150px" sortable="custom" sort-by="taskdetail"
          align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column> -->
        <!-- <el-table-column prop="responsible" label="责任人" width="100px" sortable="custom" sort-by="responsible"
          align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="responsiblecode" label="责任人工号" width="100px" sortable="custom" sort-by="responsiblecode"
          align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column> -->
        <el-table-column prop="planstartdate" label="计划开始日期" width="105px" sortable="custom" sort-by="planstartdate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"> </el-table-column>
        <el-table-column prop="actualstartdate" label="实际开始日期" width="105px" sortable="custom" sort-by="actualstartdate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"> </el-table-column>
        <el-table-column prop="planenddate" label="计划完成日期" width="105px" sortable="custom" sort-by="planenddate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"> </el-table-column>
        <el-table-column prop="actualenddate" label="实际完成日期" width="105px" sortable="custom" sort-by="actualenddate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"> </el-table-column>
        <el-table-column prop="taskdistributedate" label="任务派发日期" width="105px" sortable="custom" sort-by="taskdistributedate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"></el-table-column>
        <!-- <el-table-column label="操作" width="290px" v-slot="scope" fixed="right">
          <el-button @click="starTaskDialog('startTaskEdit', scope.row)" :disabled="scope.row.taskstate !== '0'">开始任务 </el-button>
          <el-button @click="openAddDialog('editFeedback', scope.row)" :disabled="scope.row.taskstate !== '1'">进度更新 </el-button>
          <el-button @click="openViewDialog('editFeedbackList', scope.row.id)" :disabled="scope.row.taskstate === '0'">
            历史进度
          </el-button>
        </el-table-column> -->
        <el-table-column label="操作" width="95px" v-slot="scope" fixed="right">
          <el-dropdown trigger="hover">
            <el-button type="text" icon="el-icon-s-operation" style="margin-left:10px">更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu>
              <el-dropdown-item icon="el-icon-plus" @click.native="starTaskDialog('startTaskEdit', scope.row)" :disabled="scope.row.taskstate !== '0'">开始任务</el-dropdown-item>
              <el-dropdown-item icon="el-icon-refresh" @click.native="openFeedbackDialog('editFeedback', scope.row)" :disabled="scope.row.taskstate !== '1'">进度更新</el-dropdown-item>
              <el-dropdown-item icon="el-icon-view" @click.native="openViewDialog('editFeedbackList', scope.row.id)" :disabled="scope.row.taskstate === '0'">历史进度</el-dropdown-item>
              <el-dropdown-item icon="el-icon-circle-close" @click.native="openCloseDialog('closeTaskEdit', scope.row)" :disabled="scope.row.taskstate !== '1'">异常关闭</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-table-column>
      </el-table>
      <!-- 列表分页 -->
      <el-row class="pagebar" ref="pager">
        <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
      </el-row>
      <tl-edit ref="editForm" @refresh="doRefresh"></tl-edit>
      <tl-edit-Feedback ref="editFeedback" @refresh="doRefresh"></tl-edit-Feedback>
      <tl-edit-Feedbacklist title="历史进度" ref="editFeedbackList" @refresh="doRefresh"></tl-edit-Feedbacklist>
      <tl-start-TaskEdit ref="startTaskEdit" @refresh="doRefresh"></tl-start-TaskEdit>
      <tl-close-task-edit ref="closeTaskEdit" @refresh="doRefresh"></tl-close-task-edit>
    </div>
  </div>
</template>

<script>
/** mixins */
import { mixin_list } from '@tapui/mixins/mixin_list'
/** 子组件 */
import tlEdit from './edit'
import tlStartTaskEdit from '../gpmProjectTaskFeedback/startTaskEdit'
import tlEditFeedback from '../gpmProjectTaskFeedback/edit'
import tlEditFeedbacklist from '../gpmProjectTaskFeedback/refList'
import tlCloseTaskEdit from '../gpmProjectTaskFeedback/closeTask'
/** 工具类 */
import { getAction, putAction, postAction, deleteAction } from '@tapui/utils/http'
import { filterObjectNull, rmbFormat } from '@tapui/utils/util'
export default {
  name: 'sdProjectTask',
  mixins: [mixin_list],
  components: { tlEdit, tlEditFeedback, tlEditFeedbacklist, tlStartTaskEdit, tlCloseTaskEdit },
  data() {
    return {
      description: '项目任务-列表查询',
      urlPrefix: '/sd/projectTask/',
      url: {
        queryCodeList: '/sd/projectTask/dataCodeList',
        start: '/sd/projectTask/start'
      },
      // 排序参数
      sorterInfo: {
        sidx: 'planenddate',
        sord: 'asc'
      },
      taskstateList: ['0', '1'],
      reduceHeight: 150,
      projectcode: '',
      sourceList: []
    }
  },
  computed: {
    checkTaskScheduleIsNull() {
      return (row) => {
        if (!row.taskschedule || row.taskschedule === '') {
          return true
        }
        return false
      }
    }
  },
  methods: {
    /** 打开新增对话框实现 */
    openAddDialog(editForm, rowData) {
      if (!this.beforeOpenAddDialog(editForm, rowData)) {
        return
      }
      this.$refs[editForm].taskstate = rowData.taskstate
      this.$refs[editForm].taskschedule = rowData.taskschedule
      this.$refs[editForm].planstartdate = rowData.planstartdate
      this.$refs[editForm].planenddate = rowData.planenddate
      this.$refs[editForm].taskdistributedate = rowData.taskdistributedate
      this.$refs[editForm].taskid = rowData.id
      this.$refs[editForm].taskname = rowData.taskname
      this.$refs[editForm].taskschedule = rowData.taskschedule
      this.$refs[editForm].isAdd = true
      this.$refs[editForm].visible = true
      this.$refs[editForm].disableEditSubmit = false
      this.$refs[editForm].projectcode = rowData.projectcode
      this.$refs[editForm].isdelivery = rowData.isdelivery
      this.$refs[editForm].productline = rowData.productline
      this.$refs[editForm].productbigtype = rowData.productbigtype
      this.$refs[editForm].productmiddletype = rowData.productmiddletype
      this.$refs[editForm].productname = rowData.productname
      this.$refs[editForm].amount = rowData.amount
      this.$refs[editForm].fund = rowData.fund
      this.$refs[editForm].newamount = rowData.newamount
      this.$refs[editForm].newfund = rowData.newfund
      this.$nextTick(() => {
        this.afterOpenAddDialog(editForm, rowData)
      })
    },
    /** 打开关闭任务对话框实现 */
    openAddTaskCloseDialog(editForm, rowData) {
      if (!this.beforeOpenAddDialog(editForm, rowData)) {
        return
      }
      this.$refs[editForm].taskstate = rowData.taskstate
      this.$refs[editForm].taskdistributedate = rowData.taskdistributedate
      this.$refs[editForm].taskid = rowData.id
      this.$refs[editForm].taskname = rowData.taskname
      this.$refs[editForm].taskschedule = rowData.taskschedule
      this.$refs[editForm].isAdd = true
      this.$refs[editForm].visible = true
      this.$refs[editForm].disableEditSubmit = false
      this.$refs[editForm].projectcode = rowData.projectcode
      this.$nextTick(() => {
        this.afterOpenAddDialog(editForm, rowData)
      })
    },
    /** 开始任务 */
    starTaskDialog(editForm, rowData) {
      this.$refs[editForm].visible = true
      this.$refs[editForm].taskid = rowData.id
      if (rowData.taskstate !== '0') {
        this.$tap.alert('任务不是未开始状态，无法开始！', 'warning')
        return
      }
      // this.$tap
      //   .confirm('确定开始任务吗？')
      //   .then(_ => {
      //     this.saveLoading = true
      //     postAction(this.url.start, param).then(res => {
      //       if (res.success) {
      //         this.$tap.message(res.message, 'success')
      //         this.$emit('refresh')
      //         this.loadData()
      //       } else {
      //         this.$tap.alert(res.message, 'warning')
      //       }
      //       this.saveLoading = false
      //     })
      //   })
      //   .catch(_ => {
      //     this.submitLoading = false
      //   })
    },
    /** 获取查询参数 */
    getQueryParams() {
      let param = {}
      if (this.taskstateList.length > 0) {
        param.qry_taskstate = this.taskstateList.join(',')
      }
      if (this.superQueryParams) {
        param['superQueryParams'] = encodeURI(this.superQueryParams)
      }
      Object.assign(param, this.queryParam, this.sorterInfo, this.filtersInfo)
      param.currentPage = this.pageInfo.currentPage
      param.pageSize = this.pageInfo.pageSize
      param.qry_state = 9
      param.qry_responsiblecode = this.user.code
      return filterObjectNull(param)
    },
    /** 格式化任务进度 */
    percentageFormat(row, column, cellValue, index) {
      let taskScheduleValue = row[column.property]
      if (typeof taskScheduleValue === 'undefined' || taskScheduleValue === null || taskScheduleValue === '') {
        return '0%'
      }
      taskScheduleValue = taskScheduleValue + '%'
      return taskScheduleValue
    },
    /** 改变任务状态标签颜色 */
    changeStateType(state) {
      return state === '0' ? 'info' : state === '-1' ? 'danger' : state === '2' ? 'success' : ''
    },
    // 打开任务反馈页面
    openFeedbackDialog(editForm, rowData) {
      const params = {
        taskid: rowData.id
      }
      getAction('/sd/projectTask/queryTaskFeedbackList', params).then((res) => {
        if (res.success) {
          if (res.result && res.result.length > 0) {
            if (res.result[0].state === 1) {
              this.openViewDialog(editForm, res.result[0].id)
            } else if (res.result[0].state === 0 || res.result[0].state === 2) {
              this.openEditDialog(editForm, res.result[0].id)
            } else {
              this.openAddDialog(editForm, rowData)
            }
          } else {
            this.openAddDialog(editForm, rowData)
          }
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
    },
    // 打开任务关闭页面
    openCloseDialog(editForm, rowData) {
      const params = {
        taskid: rowData.id
      }
      getAction('/sd/projectTask/queryTaskCloseList', params).then((res) => {
        if (res.success) {
          if (res.result && res.result.length > 0) {
            if (res.result[0].state === 1) {
              this.openViewDialog(editForm, res.result[0].id)
            } else if (res.result[0].state === 0 || res.result[0].state === 2) {
              this.openEditDialog(editForm, res.result[0].id)
            } else {
              this.openAddTaskCloseDialog(editForm, rowData)
            }
          } else {
            this.openAddTaskCloseDialog(editForm, rowData)
          }
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
    },
    // 加载数据后
    afterLoadData() {
      this.datas.forEach(data => {
      const params = {
        taskid: data.id
      }
      getAction('/sd/projectTask/queryTaskFeedbackList', params).then((res) => {
        if (res.success) {
          if (res.result && res.result.length > 0) {
            if (res.result[0].state === 1) {
              this.sourceList.push(res.result[0])
            }
          }
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
      })
    },
    istaskapproval(row) {
      return this.sourceList.filter((item) => parseInt(item.taskid) === row.id).length > 0
    },
    doRowDbClick(row, column, event) {
      this.openViewDialog('editForm', row[this.$refs.mainTable.rowKey])
    }
  }
}
</script>
<style scoped></style>
